package com.app.controller.authority;

import java.util.*;

import com.app.entity.Menu;
import com.app.entity.authority.Meta;
import com.app.entity.authority.SecurityUser;
import com.app.entity.authority.User;
import com.app.entity.authority.WebMenu;
import com.app.service.authority.MenuService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.app.service.authority.UserService;
/**
 * 
 * @author zhangyufei
 * ��Ա�б�
 */
@RestController
@RequestMapping("/api")
public class UserController {
	
	private static final org.slf4j.Logger log =   LoggerFactory.getLogger(UserController.class);
   
	
	
	/**
	 * �û��б�
	 */
	@Autowired
    public UserService userService ;
	 @RequestMapping(value="/queryuser",method=RequestMethod.POST)
    @ResponseBody
	public Map<String, Object> query(){
		Map<String, Object> result=new HashMap<String, Object>();
		try {
			List<User> user=userService.queryUser();
			System.out.println("user:"+user);
		   log.info("------------"+user+"------------");
			if (user!=null) {
				result.put("code",200);
				result.put("user", user);
				 log.info("--------���Ϊ��---"+user+"------------");
				 
			}
		} catch (Exception e) {
			System.out.println(e);
			result.put("code", 500);
			result.put("MSG", "��ѯʧ��");
		}
		return result;
				
	}


	@Autowired
	private MenuService menuService;



	WebMenu getWebMenu(Menu menu){

		WebMenu webmenu=new WebMenu();
		webmenu.setPath(menu.getPath());

		webmenu.setOtherUrl(menu.getOtherUrl());

		webmenu.setName(menu.getCode());
		webmenu.setComponent(menu.getComponent());
		webmenu.setRedirect(menu.getUrl());
		Meta meta=new Meta();
		meta.setTitle(menu.getName());
		meta.setIcon(menu.getIcon());
		meta.setNoCache(menu.getNoCache());
		webmenu.setMeta(meta);


		if(menu.getChildren()==null || menu.getChildren().size()==0) {
			 webmenu.setChildren(new ArrayList<WebMenu>());
			WebMenu webmenu2=new WebMenu();
			webmenu2.setPath(menu.getPath());
			webmenu2.setOtherUrl(menu.getOtherUrl());
			webmenu2.setName(menu.getCode());
			webmenu2.setComponent(menu.getComponent());
			webmenu2.setRedirect(menu.getUrl());
			webmenu2.setMeta(meta);
			webmenu.getChildren().add(webmenu2);
		}

		return  webmenu;

	}
	void LoadWebMenu(WebMenu webMenus,Menu menu){

		if(menu.getChildren()!=null
				)
		for(Menu _m :menu.getChildren()){
			WebMenu _webMenu = getWebMenu(_m);
			webMenus.getChildren().add(_webMenu);
			LoadWebMenu(_webMenu,_m);


		}

	}
	/**
	 * ��½
	 * @return
	 */
	@RequestMapping(value="/getInfo" ,method=RequestMethod.POST)
	// @ResponseBody
	public Map<String, Object> doLogin(@RequestBody User _user){


		//@RequestParam("password") String password){
		Map<String, Object> results = new HashMap<String,Object>();
//		 	String username  = m.get("username").toString();
//			String password  = m.get("password").toString();
		try {


			//User user = this.userService.login(_user.getId());
			SecurityUser u=new SecurityUser(_user);
			//List<Menu> menu = menuService.getInfo(_user.getId());

			List<Menu> menu=this.menuService.querySecurityMenuAll(_user.getId());

			List<WebMenu> webMenus=new ArrayList<>()    ;



			for (Menu m :menu){
				WebMenu  _webmenu=getWebMenu(m);
				webMenus.add(_webmenu);
				LoadWebMenu(_webmenu,m);
			}

			if(u !=null){
				results.put("code", 200);
				results.put("token", StringUtils.join(u.getRolesArray(), ","));
				results.put("menu", webMenus);

				results.put("message", "操作成功！");
				results.put("data", u);
			}
			else{
				results.put("code", 201);
				results.put("message", "用户名或密码错误！");
				results.put("data", null);
			}

			return results;
		} catch (Exception e) {


			e.printStackTrace();
			results.put("code", 500);
			results.put("message", e.getMessage());
			return results;
		}
	}

}
